package com.masterclient.app.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.eoner.common.config.Config;
import com.eoner.baselib.utils.notify.RxBus;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	RxBus rxBus;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		rxBus = RxBus.$();
		api = WXAPIFactory.createWXAPI(this, Config.APP_ID);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
//		Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);


		Log.e("errCode", "onPayFinish, errCode = " + resp.errCode);
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			if(resp.errCode==0){
				rxBus.post(Config.PAY_RESULT,Config.PAY_SUCCESS);
			}else{
				rxBus.post(Config.PAY_RESULT,Config.PAY_FAILED);
			}

		}
		switch (resp.errCode) {
			case BaseResp.ErrCode.ERR_OK:
				showToast("支付成功");
				//分享成功
				break;
			case BaseResp.ErrCode.ERR_USER_CANCEL:
//				showToast("订单已取消");
				break;
			case BaseResp.ErrCode.ERR_AUTH_DENIED:
//				showToast("支付失败");
				break;
		}
		finish();
	}

	public void showToast(String str){
		Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
	}
}